package lance.honeydew.util;

import java.io.*;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class GmailUtil {

	private static final Logger logger = Logger.getLogger(GmailUtil.class);
	
	private static final String username = "honeydew7878@gmail.com";
	private static final String password = "honey123456789";
	
	public static Properties getProperties() {
		Properties props = new Properties();

		try {
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.port", "587");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return props;
	}

	public static Boolean sendMail(String fromAddr, String toAddr, String subject, String content) {

		Boolean result = false;
		
		Properties props = getProperties();

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(fromAddr));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(toAddr));
			message.setSubject(subject);
			message.setText(content);

			Transport.send(message);

			result = true;

		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		return result;
	}
}
